package com.pd.web.controller.admin;

import com.alibaba.fastjson.JSON;
import com.pd.business.UserService;
import com.pd.constant.SysContant;
import com.pd.dao.model.User;
import com.pd.domain.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by dong peng
 * DATE : 2017/3/4
 * TIME : 18:01.
 */
@Controller
@RequestMapping("/admin/user")
public class ManageUserCtl extends ManageBaseController {

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    /**
     *查询用户
     */
    @RequestMapping(value = "/query")
    public @ResponseBody String query(){

        List<User> users = userService.query();
        ResponseVo responseVo = new ResponseVo();
        responseVo.setTip(SysContant.SUCCESS);
        responseVo.setMsg("调用成功");
        responseVo.addRetData("users",users);

        return JSON.toJSONString(responseVo);
    }

    /**
     *
     * 新增用户
     */
    @RequestMapping(value = "/add")
    public @ResponseBody String add(@RequestBody User user) {
        Integer result = userService.add(user);

        ResponseVo responseVo = new ResponseVo();
        if(result == -1) {
            responseVo.setTip(SysContant.BIZ_EXCEPTION);
            responseVo.setMsg("新增用户数据失败");
        }else {
            responseVo.setTip(SysContant.SUCCESS);
            responseVo.setMsg("新增用户数据成功");
        }
        return JSON.toJSONString(responseVo);
    }

    /**
     *
     * 修改用户
     */
    @RequestMapping(value = "/modify")
    public @ResponseBody String modify(@RequestBody User user) {
        Integer result = userService.modify(user);
        ResponseVo responseVo = new ResponseVo();
        if(result == -1) {
            responseVo.setTip(SysContant.BIZ_EXCEPTION);
            responseVo.setMsg("修改用户数据失败");
        }else {
            responseVo.setTip(SysContant.SUCCESS);
            responseVo.setMsg("修改用户数据成功");
        }

        return JSON.toJSONString(responseVo);
    }

    /**
     *
     * 删除用户
     */
    @RequestMapping(value = "/delete")
    public @ResponseBody String delete(@RequestBody User user) {
        Integer result = userService.delete(user.getUserid());
        ResponseVo responseVo = new ResponseVo();
        if(result == -1) {
            responseVo.setTip(SysContant.BIZ_EXCEPTION);
            responseVo.setMsg("删除用户数据失败");
        }else {
            responseVo.setTip(SysContant.SUCCESS);
            responseVo.setMsg("删除用户数据成功");
        }

        return JSON.toJSONString(responseVo);
    }
}
